package Functions;

import java.util.ArrayList;

import operations.FunctionOperations;

public class FalsePosition {
	private int defaultMaxIterations = 50;
	private double defaultEpsilon = 0.00001;
	private FunctionOperations myFunction;
	private int noOfIterations = 0;
	private double aprroxRoot = 0;
	private double preci = 0;
	private double beginInt=0;
	private double endInt=0;
	private double aGiven=0;
	private double bGiven=0;
	public FalsePosition() {
		// TODO Auto-generated constructor stub
	}
	public int getNoOfIts() {
		return noOfIterations;
	}

	public double approxRoot() {
		return aprroxRoot;
	}

	public double getPrecision() {
		return preci;
	}

	public String getInterval() {
		String x = "["+beginInt+","+endInt+"]";

		return x;

	}
	public double errorBound(){
		ErrorBound e = new ErrorBound();
		double error =e.calculateErrorBound(noOfIterations, myFunction,aGiven , bGiven, beginInt, endInt);
		return error;
	}
	public double[][] Falseposition(String function, double a, double b,
			double delta,int numOfItrs){
		aGiven = a;
		bGiven=b;
		ArrayList<Double> k = new ArrayList<Double>();
		ArrayList<Double> ak = new ArrayList<Double>();
		ArrayList<Double> bk = new ArrayList<Double>();
		ArrayList<Double> ck = new ArrayList<Double>();
		ArrayList<Double> fck = new ArrayList<Double>();
		if((delta!=-1)&&(numOfItrs!=-1)){
			defaultMaxIterations =numOfItrs;
			defaultEpsilon = delta;
		}
		
		myFunction= new FunctionOperations(function);

		double begin = a;
		double end = b;

		double fa = myFunction.evaluate(a);

		
		double precision =Math.abs(fa);
			while(precision>defaultEpsilon&&noOfIterations<defaultMaxIterations){
//				
				beginInt=begin;
				endInt=end;
				double fbegin = myFunction.evaluate(begin);
				double fend = myFunction.evaluate(end);
				double differenceX = end - begin;
				double differenceFx = fend - fbegin;
				double mid = end - ((fend * differenceX) / differenceFx);
				double fmid = myFunction.evaluate(mid);
				k.add(noOfIterations+0.0);
				ak.add(begin);
				bk.add(end);
				ck.add(mid);
				fck.add(fmid);
				precision = Math.abs(fmid);
				
				if(fbegin*fmid<0){
					//have opposite signs
					end = mid;
				}else if(fmid*fend<0){
					begin = mid;
				}
				noOfIterations++;
				
			}
			double[] kA = Secant.copy(k);
			double[] akA = Secant.copy(ak);
			double[] bkA = Secant.copy(bk);
			double[] ckA = Secant.copy(ck);
			double[] fckA = Secant.copy(fck);
			double[][] table = new double[k.size()][5];
	    
	        
	        for(int i =0 ; i<table.length;i++){
	        
	            table[i][0] = kA[i];
	            table[i][1] = akA[i];
	            table[i][2]= bkA[i];
	            table[i][3]= ckA[i];
	            table[i][4]= fckA[i];
	            
	        }
	        
			preci = precision;
			System.out.println(noOfIterations);
			return table;
	
	}
	public static void main(String[] args) {
		FalsePosition b = new FalsePosition();
		b.Falseposition("x*sin(x)-1", 0, 2, .01, 100);
	}
}
